package com.modules.controller.generator;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.controller.BaseController;
import com.common.log.LogData;
import com.common.vo.Result;
import com.modules.entity.generator.CodeGeneratorEntity;
import com.modules.service.generator.CodeGeneratorService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 代码生成器
 * @author yyq
 *
 */
@RestController
@RequestMapping("/generator")
public class CodeGeneratorController extends BaseController<CodeGeneratorEntity>{

	@Resource
	private CodeGeneratorService service;


	/**
	 * 分页查询
	 * @param entity
	 * @return
	 */
	@GetMapping("/select")
	public Result<?> select(CodeGeneratorEntity entity){
		LambdaQueryWrapper<CodeGeneratorEntity> queryWrapper = Wrappers.lambdaQuery();
		IPage<CodeGeneratorEntity> iPage = service.page(getPage(), queryWrapper);
		return Result.page(iPage.getRecords(), iPage.getTotal());
	}

	/**
	 * 查询单个
	 * @return
	 */
	@GetMapping("/selectOne")
	public Result<?> selectOne(){
		LambdaQueryWrapper<CodeGeneratorEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(CodeGeneratorEntity::getState, 1);
		CodeGeneratorEntity selectOne = null;
		List<CodeGeneratorEntity> selectList = service.list(queryWrapper);
		if(CollUtil.isNotEmpty(selectList)){
			selectOne = selectList.get(0);
		}
		return Result.success(selectOne);
	}

	/**
	 * 保存
	 * @param entity
	 * @return
	 */
	@LogData(module = "字典管理", remarks = "保存")
	@PostMapping("/save")
	public Result<?> save(CodeGeneratorEntity entity){
		service.saveOrUpdate(entity);
		return Result.success();
	}

	/**
	 * 删除
	 * @return
	 */
	@LogData(module = "代码生成器配置", remarks = "删除单个")
	@DeleteMapping("/delete")
	public Result<?> delete(String id){
		int count = service.count();
		if(count < 2){
			return Result.fail("至少保留一条");
		}
		service.removeById(id);
		return Result.success();
	}

	/**
	 * 修改启用状态
	 * @return
	 */
	@PostMapping("/updateState")
	public Result<?> updateState(CodeGeneratorEntity entity){
		LambdaQueryWrapper<CodeGeneratorEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(CodeGeneratorEntity::getState, 1);
		if (entity.getState() == 0){
			int count = service.count(queryWrapper);
			if(count == 0){
				return Result.fail("至少启用一条");
			}
		}
		CodeGeneratorEntity generatorEntity = new CodeGeneratorEntity();
		generatorEntity.setState(0);
		service.update(generatorEntity, queryWrapper);
		service.updateById(entity);
		return Result.success();
	}


	/**
	 * 表名分页查询
	 * @return
	 */
	@GetMapping("/selectTablePage")
	public Result<?> selectTablePage(
							@RequestParam(value="page",defaultValue="1",required=true) Integer page,
							@RequestParam(value="limit",defaultValue="10",required=true) Integer limit){
		return service.selectTablePage(new Page<>(page, limit));
	}

	/**
	 * 字段分页查询
	 * @return
	 */
	@GetMapping("/selectTableFieldPage")
	public Result<?> selectTableFieldPage(@RequestParam(value="name") String name){
		return service.selectTableFieldPage(name);
	}

	/**
	 * 生成代码-单个
	 * @return
	 */
	@PostMapping("/createOneCode")
	public Result<?> createOneCode(@RequestParam(value="tableNames") String tableNames,
							   @RequestParam(value="fieldNames") String fieldNames,
							   @RequestParam(value="tableComments") String tableComments){
		return service.createOneCode(tableNames, fieldNames, tableComments);
	}

	/**
	 * 生成代码-多选表
	 * @return
	 */
	@PostMapping("/createCodeTables")
	public Result<?> createCodeTables(@RequestParam(value="tableNames") String tableNames,
									 @RequestParam(value="tableComments") String tableComments){
		return service.createCodes(tableNames, tableComments);
		//return service.createCodeTables(tableNames, tableComments);
	}




}
